package com.web.words;

import com.bean.User;
import com.bean.Words;
import com.service.WordsService;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ListWordsServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletContext application= req.getServletContext();
        WordsService service = new WordsService();
        List<Words> LW=service.getAllwords();
        req.getSession().setAttribute("ListWord",LW);
        System.out.println(LW);

        try{
            int num=service.getWordCount();
            req.getSession().setAttribute("wordNum",num);
        }catch (Exception e){
            e.printStackTrace();
        }

        //User user = (User) application.getAttribute("user");
        User user =(User)req.getSession().getAttribute("user");
        if(user==null){
            req.getRequestDispatcher("/front/word/WordShow.jsp").forward(req,resp);
        }else{
            req.getRequestDispatcher("/admin/word/list.jsp").forward(req,resp);
        }

    }
}
